package org.aiforum.backend.controller;

import java.util.List;

import org.aiforum.backend.common.QueryPreperty;
import org.aiforum.backend.common.Response;
import org.aiforum.backend.service.TopicService;
import org.aiforum.domain.Reply;
import org.aiforum.domain.Topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 帖子控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/topic")
public class TopicController {
   
	@Autowired
	private TopicService topicService;
	
	@RequestMapping(value={"/query"})
    public ModelAndView query(QueryPreperty queryPreperty,PageInfo page){
    	
    	PageHelper.startPage(page.getPageNum(), page.getPageSize(), true);
    	
    	List<Topic> topics=topicService.queryByPreperty(queryPreperty);
    	page=new PageInfo<Topic>(topics);
    	ModelAndView modelAndView=new ModelAndView();
    	modelAndView.setViewName("topic/list");
    	modelAndView.addObject("topics", topics);
    	modelAndView.addObject("queryPreperty", queryPreperty);
    	modelAndView.addObject("page", page);
    	return modelAndView;
    }
    
    @RequestMapping(value={"/find/{topicId}"},method={RequestMethod.GET})
    public ModelAndView find(@PathVariable Integer topicId){
    	Topic topic=topicService.find(topicId);
    	ModelAndView modelAndView=new ModelAndView();
    	modelAndView.setViewName("topic/details");
    	modelAndView.addObject("topic", topic);
    	return modelAndView;
    }
    
    @RequestMapping(value={"/delete/{topicId}"},method={RequestMethod.GET})
    public ModelAndView delete(@PathVariable Integer topicId){
        Response response=topicService.delete(topicId);
    	ModelAndView modelAndView=new ModelAndView();
    	modelAndView.setViewName("redirect:../../message");
    	modelAndView.addObject("message", response.getMessage());
    	return modelAndView;
    }
}
